﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RadarA
{
    public partial class UserControl6vongtron : UserControl
    {
        RadaA1 radar;
        int speed = 200;
        MucTieu mb;
        List<ToaDo> qd;
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
        System.Windows.Forms.Timer t2 = new System.Windows.Forms.Timer();
        public static ListView lvTarget = new ListView();
        public static float culy;
        public static float phuongvi;
        string pathicon = string.Empty;
        private bool Updatevitri;
        //private List<MucTieu> listTarget = new List<MucTieu>();
        private string controldangchon;
        private string strNumericUpDown;
        private bool luachon;
        int solanluachon = 0;
        private bool muctieuBam;
        public UserControl6vongtron()
        {
            InitializeComponent();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
            t.Enabled = true; 
        }
        void t_Tick(object sender, EventArgs e)
        {
            double max_angle = radar.maxAngle * 0.0174532925d;

            if (clsCommons.lstTarget.Count == 0)
            {

            }
            else
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                // select which of the three items to update
                foreach (MucTieu item in clsCommons.lstTarget)
                {
                    //khoang cach di duoc cua muc tieu trong 1 s
                    //double s = item._current.velocity * 1000 / (3600000 * 3);
                    double s = item._current.velocity * 1000 / (3600 * 30);

                    //toa do tuong doi giua diem di va diem den
                    double x = item._to.X - item._current.X;
                    double y = item._to.Y - item._current.Y;
                    double chenhlech_kc = Math.Sqrt(x * x + y * y);
                    double gocngang = Math.Acos(x / chenhlech_kc);
                    //doi sang do.
                    gocngang = gocngang / 0.0174532925d;

                    if (y < 0)
                    {
                        gocngang = 360d - gocngang;
                    }
                    //doi sang radian de tinh sin va cos
                    gocngang *= 0.0174532925d;
                    double z = item._to.Z - item._current.Z;
                    double gocdung = Math.Asin(z / Math.Sqrt(z * z + chenhlech_kc * chenhlech_kc));
                    //toa do muc tieu sau 1s
                    double z1 = item._current.Z + (s * Math.Sin(gocdung));
                    double chenhlech_kc1 = (float)(s * Math.Abs(Math.Cos(gocdung)));

                    double x1 = item._current.X + chenhlech_kc1 * Math.Cos(gocngang);
                    double y1 = item._current.Y + chenhlech_kc1 * Math.Sin(gocngang);
                    int dKm = item._current._Dkm;
                    ToaDo current = new ToaDo(item._current.stt, x1, y1, dKm, z1, item._current._Hkm, item._current.velocity, "abc");
                    //gan diem nay thanh diem bat dau
                    item._current = current;
                    //_radar.AddItem(item);

                    //_radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                    if (radar._az + 20 > RadaA1.getAzimuth(current) && radar._az - 20 < RadaA1.getAzimuth(current) && RadaA1.getRange(current) <= pictureBox1.Width / 2 && (RadaA1.getRange(item._current) * Math.Sin(max_angle)) > item._current.Z)
                    {


                        bool Flag = true;
                        for (int j = 0; j < lvTarget.Items.Count; j++)
                        {

                            if (lvTarget.Items[j].Text.Trim() == item.ID.ToString().Trim())
                            {
                                ListViewItem lvItem = lvTarget.Items[j];
                                lvItem.Text = item.ID.ToString();
                                lvItem.SubItems[1].Text = (item._category);
                                lvItem.SubItems[2].Text = (RadaA1.getAzimuth(item._current).ToString());

                                int rg = (int)RadaA1.getRange(item._current);

                                lvItem.SubItems[3].Text = (rg.ToString());
                                lvItem.SubItems[4].Text = (item._current.Z.ToString());
                                lvItem.SubItems[5].Text = (item._current.velocity.ToString());
                                Flag = false;
                                if (Form1.muctieuBam)
                                {
                                    //textBox3.Text = Convert.ToInt16(getAzimuth(Convert.ToInt16(item._to.X), Convert.ToInt16(item._to.Y), pictureBox1.Width)).ToString();
                                    //textBox1.Text = item._current._Dkm.ToString();
                                    //textBox7.Text = item._current.Z.ToString();
                                }
                                int abc = checkmousemove(Convert.ToInt16(item._current.X), Convert.ToInt16(item._current.Y), pictureBox1.Width);
                                if (abc == 240)
                                {
                                    lvTarget.Items.RemoveByKey(item.ID.ToString());
                                }
                                else
                                {
                                    radar.AddItem(item);
                                }
                                //radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
                            }
                        }
                        if (Flag)
                        {
                            ListViewItem lvItem = new ListViewItem();
                            lvItem.Text = item.ID.ToString();
                            lvItem.SubItems.Add(item._category);
                            lvItem.SubItems.Add(RadaA1.getAzimuth(item._current).ToString());

                            int rg = (int)RadaA1.getRange(item._current);

                            lvItem.SubItems.Add(rg.ToString());
                            lvItem.SubItems.Add(item._current.Z.ToString());
                            lvItem.SubItems.Add(item._current.velocity.ToString());
                            lvItem.BackColor = item._color;
                            lvTarget.Items.Add(lvItem);
                        }
                    }
                }
            }
        }

        private void UserControl6vongtron_Load(object sender, EventArgs e)
        {
            clsCommons.sizePictureBox = pictureBox1.Width;
            pathicon = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
            //radar = new RadaA1(pictureBox1.Width - 5, 344, 50, 285, 6);
            radar = new RadaA1(pictureBox1.Width - 5,6);
            // radar.Sovongtron = 3;
            pictureBox1.Image = radar.Image;
            radar.pathIcon = false;
            radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
            radar.DrawScanInterval = speed / 20;
            //RadaA1.TatmoAnten = false;
            ChinhlaiVitriRadar();
        }

        private void ChinhlaiVitriRadar()
        {
            pictureBox1.Width = pictureBox1.Width - 260;
            pictureBox1.Location = new Point(pictureBox1.Location.X + 100, pictureBox1.Location.Y);
        }

        private void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e)
        {
            pictureBox1.Image = e.Image;
        }

        void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            toado.Text = "Cự ly: " + e.Location.X.ToString() + " Phương vị " + e.Location.Y.ToString();
            int abc = checkmousemove(e.Location.X, e.Location.Y, pictureBox1.Width);


            if (abc <= 240)
            {
                int ab = getAzimuth(e.Location.X, e.Location.Y, pictureBox1.Width);
                toado.Text = "Cự ly: " + e.Location.X.ToString() + " Phương vị " + e.Location.Y.ToString();
                lblPhuongVi.Text = "hướng: " + ab.ToString();
                lblCuLy.Text = "Db:" + Convert.ToInt16(getRange(e.Location.X, e.Location.Y, pictureBox1.Width)).ToString();
            }
        }
        private int getAzimuth(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            //tinh goc so voi truc x
            double angle = Math.Acos(x / canh_huyen);
            //doi sang do.
            angle = angle / 0.0174532925d;

            if (y < 0)
            {
                angle = 360d - angle;
            }
            //chuyen sang goc so voi truc. y - Phuong BAC'

            double azimuth = angle - 270d;
            if (azimuth < 0) azimuth = azimuth + 360d;
            return (int)azimuth;
        }
        public PointF huong(int azimuth, int widthPictrueBox)
        {
            int _size = widthPictrueBox;
            //chuyen goc phuong vi sang goc theo truc X
            double angle = (270d + (double)azimuth);

            // chuyen sang radian
            angle *= 0.0174532925d;

            double r, x, y;

            // tinh x va y theo size cua man hinh scan radar co san
            r = _size * 0.5;

            //_size = pictureBox1.Width;

            x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
            y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));

            return new PointF((float)x, (float)y);
        }

        private int checkmousemove(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return Convert.ToInt16(canh_huyen);
        }
        //hàm tính vị trí so với tâm
        public static double getRange(int _x, int _y, int size)
        {
            double r, x, y;
            r = size * 0.5d;//ban kinh duong tron
            //xac dinh toa do so voi tam
            x = _x - r;
            y = _y - r;
            double canh_huyen = Math.Sqrt(x * x + y * y);
            return Convert.ToInt16(canh_huyen) / 8;
        }
    }
}
